package com.ruoyi.web.controller.djb;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.djb.domain.DjbAdmin;
import com.ruoyi.djb.domain.DjbDistrict;
import com.ruoyi.djb.domain.DjbRegistry;
import com.ruoyi.djb.domain.DjbUser;
import com.ruoyi.djb.mapper.DjbAdminMapper;
import com.ruoyi.djb.mapper.DjbDistrictMapper;
import com.ruoyi.djb.mapper.DjbReleaseMapper;
import com.ruoyi.djb.mapper.DjbUserMapper;
import com.ruoyi.djb.service.IDjbRegistryService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登记管理Controller
 * 
 * @author xxd
 * @date 2023-12-08
 */
@Controller
@RequestMapping("/djb/registry")
public class DjbRegistryController extends BaseController
{
    private String prefix = "djb/registry";

    @Autowired
    private IDjbRegistryService djbRegistryService;
    @Resource
    private DjbDistrictMapper districtMapper;
    @Resource
    private DjbUserMapper userMapper;
    @Resource
    private DjbReleaseMapper releaseMapper;
    @Resource
    private DjbAdminMapper adminMapper;

    @RequiresPermissions("djb:registry:view")
    @GetMapping()
    public String registry()
    {
        return prefix + "/registry";
    }

    /**
     * 查询登记管理列表
     */
    @RequiresPermissions("djb:registry:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjbRegistry djbRegistry) {
        startPage();
        djbRegistry.setExamFlag("");
        djbRegistry.setDeleteFlag(1);
        List<DjbRegistry> list = djbRegistryService.selectDjbRegistryList(djbRegistry);
        for(DjbRegistry tmp : list){
            DjbDistrict djbDistrict = districtMapper.selectDjbDistrictById(tmp.getDistrictId());
            if(djbDistrict!=null){
                tmp.setDistrictName(djbDistrict.getName());
            }
//            if(tmp.getStatus().equals(1)){
//                DjbRelease release = new DjbRelease();
//                release.setRegId(tmp.getId());
//                List<DjbRelease> djbReleaseList = releaseMapper.selectDjbReleaseList(release);
//                if(!djbReleaseList.isEmpty()&&djbReleaseList.get(0).getOutTime()!=null){
//                    tmp.setStatus(7);
//                }
//            }
        }
        return getDataTable(list);
    }

    /**
     * 导出登记管理列表
     */
    @RequiresPermissions("djb:registry:export")
    @Log(title = "登记管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DjbRegistry djbRegistry) {
        List<DjbRegistry> list = djbRegistryService.selectDjbRegistryList(djbRegistry);
        ExcelUtil<DjbRegistry> util = new ExcelUtil<DjbRegistry>(DjbRegistry.class);
        return util.exportExcel(list, "登记管理数据");
    }

    /**
     * 新增登记管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存登记管理
     */
    @RequiresPermissions("djb:registry:add")
    @Log(title = "登记管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DjbRegistry djbRegistry)
    {
        return toAjax(djbRegistryService.insertDjbRegistry(djbRegistry));
    }

    /**
     * 修改登记管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        DjbRegistry djbRegistry = djbRegistryService.selectDjbRegistryById(id);
        mmap.put("djbRegistry", djbRegistry);
        DjbUser djbUser = userMapper.selectDjbUserById(djbRegistry.getUserId());

        return prefix + "/edit";
    }

    /**
     * 修改保存登记管理
     */
    @RequiresPermissions("djb:registry:edit")
    @Log(title = "登记管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DjbRegistry djbRegistry) {
        return toAjax(djbRegistryService.updateDjbRegistry(djbRegistry));
    }

    /**
     * 删除登记管理
     */
    @RequiresPermissions("djb:registry:remove")
    @Log(title = "登记管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(djbRegistryService.deleteDjbRegistryByIds(ids));
    }

    /**
     * 首页分析
     */
    @GetMapping("/analysisPlat")
    @ResponseBody
    public AjaxResult analysisPlat(){
        SysUser user = ShiroUtils.getSysUser();
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtils.getNowDate();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        /*昨天0点*/
        Date todayZero = calendar.getTime();
        Date endTime=todayZero;
        Date startTime =DateUtils.addDays(todayZero,-30);

        Map<String, Object> params=new HashMap<>();

        int diff =DateUtils.differentDaysByMillisecond(startTime,endTime);
        List<String> Xais= new ArrayList<>();
        for(int i=0;i<diff;i++){
            Date new_date = DateUtils.addDays(startTime,i);
            Xais.add(new_date.getMonth()+1+"."+new_date.getDate());
        }
        params.put("beginTime", DateFormatUtils.format(startTime,DateUtils.YYYY_MM_DD));
        params.put("endTime",DateFormatUtils.format(endTime,DateUtils.YYYY_MM_DD));

        DjbRegistry registry = new DjbRegistry();
        registry.setParams(params);
        List<DjbRegistry> djbRegistries = djbRegistryService.selectDjbRegistryList(registry);

        DjbDistrict djbDistrict = new DjbDistrict();
        djbDistrict.setParams(params);
        List<DjbDistrict> djbDistricts = districtMapper.selectDjbDistrictList(djbDistrict);

        DjbAdmin admin = new DjbAdmin();
        admin.setParams(params);
        List<DjbAdmin> djbAdmins = adminMapper.selectDjbAdminList(admin);

        DjbUser djbUser = new DjbUser();
        djbUser.setParams(params);
        List<DjbUser> djbUsers = userMapper.selectDjbUserList(djbUser);


        int call_num=0;//拨打量
        int answer_num = 0;//接通量
        int collect_num = 0;//有效客户
        int call_time =0;/*通话时长*/


        JSONObject jo = new JSONObject();
        jo.put("call_num",call_num);
        jo.put("answer_num",answer_num);
        jo.put("collect_num",collect_num);
        jo.put("call_time",call_time);
        List<Integer> callData= new ArrayList<>();
        List<Integer> answerData= new ArrayList<>();
        List<Integer> collectData= new ArrayList<>();
        List<Integer> callTimeData= new ArrayList<>();
        for(int i=0;i<diff;i++){
            Date new_date = DateUtils.addDays(startTime,i);
            Date new_date_end = DateUtils.addDays(startTime,i+1);
            int call_sub=0;
            for(DjbRegistry tmp : djbRegistries){
                if(tmp.getCreateTime().before(new_date)||tmp.getCreateTime().after(new_date_end)){
                    continue;
                }
                call_sub++;
            }
            callData.add(call_sub);

            int answer_sub=0;
            for(DjbDistrict tmp : djbDistricts){
                if(tmp.getCreateTime().before(new_date)||tmp.getCreateTime().after(new_date_end)){
                    continue;
                }
                answer_sub++;
            }
            answerData.add(answer_sub);

            int collect_sub=0;
            for(DjbAdmin tmp : djbAdmins){
                if(tmp.getCreateTime().before(new_date)||tmp.getCreateTime().after(new_date_end)){
                    continue;
                }
                collect_sub++;
            }
            collectData.add(collect_sub);

            int admin_sub=0;
            for(DjbUser tmp : djbUsers){
                if(tmp.getCreateTime().before(new_date)||tmp.getCreateTime().after(new_date_end)){
                    continue;
                }
                admin_sub++;
            }
            callTimeData.add(admin_sub);
        }
        jo.put("Xais",Xais);
        jo.put("callData",callData);
        jo.put("answerData",answerData);
        jo.put("collectData",collectData);
        jo.put("callTimeData",callTimeData);

        jo.put("new_reg",callData.get(callData.size()-1));
        jo.put("new_district",answerData.get(answerData.size()-1));
        jo.put("new_admin",collectData.get(collectData.size()-1));
        jo.put("new_user",callTimeData.get(callTimeData.size()-1));

        jo.put("all_district",djbDistricts.size());
        jo.put("all_admin",djbAdmins.size());
        jo.put("all_user",djbUsers.size());

        return AjaxResult.success(jo);
    }
}
